package com.mengze.linlee_iot_adnroid.ui.dashboard

import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.mengze.linlee_iot_adnroid.adapter.ProductAdapter
import com.mengze.linlee_iot_adnroid.adapter.ProductCategoryAdapter
import com.mengze.linlee_iot_adnroid.databinding.FragmentDashboardBinding

class DashboardFragment : Fragment() {

    private lateinit var dashboardViewModel: DashboardViewModel
    private var _binding: FragmentDashboardBinding? = null
    private val binding get() = _binding!!
    private lateinit var categoryAdapter: ProductCategoryAdapter
    private lateinit var productAdapter: ProductAdapter
    private val categories = listOf("电子产品", "家居用品", "食品饮料", "服装鞋帽")
    private val productMap = mapOf(
        "电子产品" to listOf("手机", "笔记本", "平板"),
        "家居用品" to listOf("沙发", "床垫", "灯具"),
        "食品饮料" to listOf("矿泉水", "饼干", "牛奶"),
        "服装鞋帽" to listOf("T恤", "牛仔裤", "运动鞋")
    )


    private fun updateStatusBarAndNavBar( inflater: LayoutInflater, container: ViewGroup?): View {
        val activity = getActivity();
        // 方法1：直接设置颜色值
        activity?.window?.statusBarColor = Color.parseColor("#FF54D437") // 橙色
        activity?.window.apply {
            var statusBarColor = Color.parseColor("#FF54D437") // 设置颜色
        }

        _binding = FragmentDashboardBinding.inflate(inflater, container, false)
        val root: View = binding.root
        binding.toolbarTitle.text = "产品列表"
        return root;
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        var root:View = updateStatusBarAndNavBar( inflater, container )

        /***
         * 左侧分类列表
          */
        val rvCategories =  binding.rvCategories;
        categoryAdapter = ProductCategoryAdapter(categories) { selectedCategory ->
//            productAdapter.updateProducts(productMap[selectedCategory] ?: emptyList())
        }
        rvCategories.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = categoryAdapter
        }


        /**
         *  右侧产品列表
         * */
        val rvProducts = binding.rvProducts
        productAdapter = ProductAdapter(emptyList()){
            selectProductItem -> {

            }
        }
        rvProducts.apply {
            layoutManager = GridLayoutManager(context, 2)
            adapter = productAdapter
        }

        // 默认显示第一个分类
        categoryAdapter.selectFirstItem()

        return root
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }


}
